let app = getApp();
Page({
  data: {
    navFlag: 0,
    indexImg: [],
    openid: null,
    homeData: null,
    //定义一个数组，数组每一项是对象，对象里面对应菜单的数据
    //管理菜单的每一项的不同参数，还有不同布局结构需要的额外的参数
    navData: [],
    baokuangArr: [],
    //服务列表项目
    tabBarlist: [{
      text: "粤港品牌",
      img: "/img/tabBarImg/home_a.png",
      url: '/pages/brand/brand'
    }, {
      text: "服务中心",
      img: "/img/tabBarImg/vip_a.png",
      url: '/pages/serviceCenter/serviceCenter'
    }, {
      text: "我的收藏",
      img: "/img/tabBarImg/order_a.png",
      url: '/pages/collect/collect'
    }, {
      text: "关于我们",
      img: "/img/tabBarImg/me_a.png",
      url: '/pages/aboutUs/aboutUs'
    }],
    recommend: [],
    baokuangID: 4,
    dataInfo: [], // 重组数组内容
    indicatorDots: true,
    baokuangBigArr: [],
    vertical: true,
    autoplay: true,
    interval: 4000,
    duration: 1500
  },
  //搜索事件
  wxSearchConfirm(res) {
    wx.navigateTo({
      url: '/pages/shoplist/shoplist?type=getbyor&areaId=&name=' + res.detail.value,
    });
  },
  //爆款查看更多
  BKseeMore(e) {
    wx.navigateTo({
      url: '/pages/baokuanDetail/baokuanDetail?currid=' + e.currentTarget.id,
    });
  },
  //区域查看更多
  seeMore(res) {
    wx.navigateTo({
      url: '/pages/shoplist/shoplist?type=getbyquyu&areaId=' + res.currentTarget.id,
    });
  },
  //单个商品详情
  detailJump(res) {
    let shopId = res.currentTarget.id;
    wx.navigateTo({
      url: '/pages/detail/detail?shopId=' + shopId,
    });
  },
  jumpCenter(event) {
    let url = event.currentTarget.id;
    wx.navigateTo({
      url: url,
    })
  },
  navJumpPage(event) {
    let url = event.currentTarget.id;
    if (url) {
      if (url === '/pages/addReport/addReport') {
        wx.navigateTo({
          url: url
        });
      } else {
        wx.redirectTo({
          url: url
        });
      }
    }
  },
  /** 
   * name:文本
   * current:是否是当前页，0不是  1是
   * style:是否为添加汇报 0不是 1是
   * ico：不同图标
   * fn：点击跳转的函数
   * id：用于判断跳转页面用的id标识
   * **/
  onShow() {
    let that = this;
    // wx.showLoading();
    wx.getStorage({
      key: 'openid',
      success: (res) => {
        if (res.data) {
          that.setData({
            openid: res.data
          })
        };
      },
    });
    app.get('GetLunBo', {
      action: 'gethaibao'
    }).then((res) => {
      that.setData({
        indexImg: res
      });
    });
    app.get('GetShangPin', {
      action: 'getShouYe'
    }).then((res) => {
      let resData = [],
        baokuangArr = [],
        baokuangBigArr = [],
        listurl = '',
        oldRes = null,
        newArr = [];
      wx.hideLoading();
      for (let i = 0; i < res.length; i++) {
        let str = res[i].leibie;        
        if (str.indexOf("爆款") != -1 ) {
          let baokuanData = res[i].data[0];          
          baokuangBigArr = {
            id: baokuanData.id,
            url: baokuanData.listurl[0].Url
          }
          listurl = res[i].data;
          listurl.splice(0, 1);
          res[i].oldRes = baokuangBigArr;
          baokuangArr.push(res[i]);
        } else if (str.indexOf("爆款") == -1) {
          resData.push(res[i]);
        }
      }
      that.setData({
        baokuangBigArr: baokuangBigArr,
        baokuangID: res[0].data[0] ? res[0].data[0].QuYuiLeiBieID:'',
        homeData: resData,
        recommend: res[0].data,
        baokuangArr: baokuangArr
      });
    })
  },
  onLoad(options) {
    let salOpenid = options.salesmanOpenid;
    if (salOpenid) {
      wx.setStorage({
        key: 'salesmanOpenid',
        data: salOpenid,
      });
    }
    this.navLoad();
  },
  onShareAppMessage() {
    return {
      title: '艾森粤港',
      path: 'pages/home/home?salesmanOpenid=' + this.data.openid,
    }
  },
  navLoad() {
    let that = this;
    wx.getStorage({
      key: 'navFlag',
      success(res) {
        let navFlag = res.data;
        if (navFlag == '客户') {
          that.setData({
            navData: [{
              name: "首页",
              current: 1,
              style: 0,
              ico: 'icon-deng',
              fn: 'navJumpPage',
              id: ''
            }, {
              name: "分类",
              current: 0,
              style: 0,
              ico: 'icon-fenlei',
              fn: 'navJumpPage',
              id: '/pages/classify/classify'
            }, {
              name: "购物车",
              current: 0,
              style: 0,
              ico: 'icon-gouwuche01',
              fn: 'navJumpPage',
              id: '/pages/cart/cart'
            }, {
              name: "我的",
              current: 0,
              style: 0,
              ico: 'icon-dkw_wode',
              fn: 'navJumpPage',
              id: '/pages/mine/mine'
            }]
          });
        } else if (navFlag == '业务员') {
          that.setData({
            navData: [{
              name: "首页",
              current: 1,
              style: 0,
              ico: 'icon-deng',
              fn: 'navJumpPage',
              id: ''
            }, {
              name: "客户",
              current: 0,
              style: 0,
              ico: 'icon-guanlikehu',
              fn: 'navJumpPage',
              id: '/pages/client/client'
            }, {
              name: "添加汇报",
              current: 0,
              style: 1,
              ico: '',
              fn: 'navJumpPage',
              id: '/pages/addReport/addReport'
            }, {
              name: "工作汇报",
              current: 0,
              style: 0,
              ico: 'icon-xiebaogao',
              fn: 'navJumpPage',
              id: '/pages/report/report'
            }, {
              name: "我的",
              current: 0,
              style: 0,
              ico: 'icon-dkw_wode',
              fn: 'navJumpPage',
              id: '/pages/salesmanMine/salesmanMine'
            }]
          });
        }
      },
    })
  },
})